<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/14 0014
 * Time: 下午 13:36
 */

namespace Home\Model;
use Think\Model;

class OnlineModel extends Model{

    //获取当前userid的所有聊天记录
    public function get_chat_jl($userid,$type){
        if($type == 'yonghu'){
            $sql = "select a.kefu_id id,b.name,a.user_wd_num num from online a
                    join users b on a.kefu_id=b.userid
                    where a.user_id='{$userid}'";
        }elseif($type == 'kefu'){
            $sql = "select a.user_id id,b.name,a.kefu_wd_num num from online a
                    join users b on a.user_id=b.userid
                    where a.kefu_id='{$userid}'";
        }

        $info = D()->query($sql);
        //dump($info);exit;
        return $info;
    }

    //查看当前客户和客服是否有聊天记录
    public function search($send_id,$kefu_id){
        $where['user_id'] = $send_id;
        $where['kefu_id'] = $kefu_id;
        $info = $this->where($where)->find();
        return $info;

    }
    //更改当前客户和客服的聊天状态
    public function update_online($userid,$kefuid,$user_type){
        $where['user_id'] = $userid;
        $where['kefu_id'] = $kefuid;

        $data[$user_type] = 1;
        $this->where($where)->save($data);
    }

    //查看当前客户和客服的聊天状态
    public function get_useridonline($userid,$kefuid,$user_type){
        $where['user_id'] = $userid;
        $where['kefu_id'] = $kefuid;
        $info = $this->field($user_type)->where($where)->find();
        return $info[$user_type];
    }

    //更改当前用户的聊天状态为离线(进入聊天窗口的上一个页面执行)
    public function upd_user_online($userid,$user_type){
        $where= [];
        $data = [];
        if($user_type == 'yonghu'){
            $where['user_id'] = $userid;
            $data['user_type'] = 0;
        }else if($user_type == 'kefu'){
            $where['kefu_id'] = $userid;
            $data['kefu_type'] = 0;
        }
        $this->where($where)->save($data);
    }

    //追加未读消息
    public function update_addto_num($userid,$kefuid,$num_type){
        $where = "user_id='{$userid}' and kefu_id='{$kefuid}'";

        $sql = "update online set {$num_type}={$num_type}+1 where {$where}";

        $info = $this->execute($sql);
        return ($info);
    }

    //未读消息清零
    public function init_num($userid,$kefuid,$num_type){
        $where = "user_id='{$userid}' and kefu_id='{$kefuid}'";
        $data[$num_type] = 0;
        $this->where($where)->save($data);
    }


    //获取当前用户的所有未读消息数量
    public function get_wd_znum($userid,$type){
        $where = '';
        $sum = '';
        $group ='';
        if($type == 'kehu'){
            $where = "user_id='{$userid}'";
            $sum = 'user_wd_num';
            $group = 'user_id';
        }else if($type == 'kefu'){
            $where = "kefu_id='{$userid}'";
            $sum = 'kefu_wd_num';
            $group = 'kefu_id';
        }
        $sql = "select sum({$sum}) count from online where {$where} group by $group";
        //dump($sql);exit;
        $info = $this->query($sql);
        return $info[0]['count'];
    }
}